package cn.ljl.org.controller;

import cn.ljl.basic.annotation.PreAuthorize;
import cn.ljl.basic.util.AjaxResult;
import cn.ljl.basic.util.PageList;
import cn.ljl.org.domain.Employee;
import cn.ljl.org.query.EmployeeQuery;
import cn.ljl.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：员工
 */
@Api(value = "员工接口类",description = "处理员工的CRUD操作的")
@RestController //前端分离项目都是同时异步请求 - 异步请求都需要加上@ResponseBody
@RequestMapping(value = "/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    /**
     * 接口：查询单个对象
     * @param id
     * @return
     */
    //前端：get http://localhost:8080/employee/1
    @ApiOperation(value = "接口：查询单个对象")
    @GetMapping(value = "/{id}")
    @PreAuthorize(name = "查询单个员工",sn = "employee:getOne")
    public Employee findOne(@PathVariable("id")Long id){
        return employeeService.loadById(id);
    }

    /**
     * 接口：分页和高级查询
     * @param employeeQuery
     * @return
     */
    //前端 post http://localhost:8080/employee json对象{}
    @ApiOperation(value = "接口：分页和高级查询")
    @PostMapping
    @PreAuthorize(name = "员工分页查询",sn = "employee:list")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.queryPage(employeeQuery);
    }

    /**
     * 接口：查询所有
     * @return
     */
    //前端：get http://localhost:8080/employee
    @ApiOperation(value = "接口：查询所有")
    @GetMapping
    @PreAuthorize(name = "员工查询所有",sn = "employee:getAll")
    public List<Employee> findAll(){
        return employeeService.loadAll();
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    //前端：delete http://localhost:8080/employee/1
    @ApiOperation(value = "接口：删除")
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "员工删除",sn = "employee:delete")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            employeeService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            //return new AjaxResult(false,"删除失败");
            return AjaxResult.me().setSuccess(false).setMsg("删除失败");
        }
    }

    /**
     * 添加或修改
     * @param employee
     * @return
     */
    //前端：put http://localhost:8080/employee/ json对象{}
    @ApiOperation(value = "接口：添加或修改")
    @PutMapping
    @PreAuthorize(name = "员工保存",sn = "employee:save")
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId()==null){
                employeeService.save(employee);
            }else{
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }


    @ApiOperation(value = "接口：批量删除")
    @PatchMapping
    @PreAuthorize(name = "员工批量删除",sn = "employee:patchDel")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            //1.调用service批量删除
            employeeService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            //return new AjaxResult(false,"删除失败");
            return AjaxResult.me().setSuccess(false).setMsg("删除失败");
        }
    }
    @GetMapping("/list/{shopId}")
    public List<Employee> queryByShopId(@PathVariable("shopId")Long shopId){
        return employeeService.queryByShopId(shopId);
    }
}
